package com.lzb.play.adapter;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.UriUtils;
import com.blankj.utilcode.util.Utils;
import com.lzb.play.R;
import com.lzb.play.basic.media.IAudioPlayer;
import com.lzb.play.bean.SelBirdBean;
import com.lzb.play.view.MyLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XiaoDan
 * @Date: 2023/12/26
 * @Description:
 */
public class SelBirdAdapter extends RecyclerView.Adapter<SelBirdAdapter.ViewHolder> {

    public List<SelBirdBean> mBirdBeans = new ArrayList<>();
    private IAudioPlayer mAudioPlayer;
    public IAudioPlayer mCurAudioPlayer;
    private int position = -1;

    public SelBirdAdapter(List<SelBirdBean> selBirdBeans, IAudioPlayer audioPlayer) {
        mBirdBeans = selBirdBeans;
        mAudioPlayer = audioPlayer;
        mAudioPlayer.setListener(mAudioPlayListener);
    }

    public int getPosition() {
        return position;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sel_bird, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bindData(mBirdBeans.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mBirdBeans != null ? mBirdBeans.size() : 0;
    }

    private ViewHolder mPlayHolder;

    private void handlePlayPause(ViewHolder holder, int position) {
        if (mPlayHolder == holder) {
            if (mCurAudioPlayer.isPlaying()) {
                mCurAudioPlayer.pause();
            }
            if (mAudioPlayer.isPlaying()) {
                mAudioPlayer.pause();
            } else {
                mAudioPlayer.resume();
            }
            return;
        }
        if (mPlayHolder != null) {
            mPlayHolder.mIvPlayPause.setImageResource(R.drawable.icon_play2);
            mPlayHolder.mIvPlayPause.setVisibility(View.INVISIBLE);
            mPlayHolder.mRlImg.setVisibility(View.VISIBLE);
            mPlayHolder.mIvPlayBg.setVisibility(View.GONE);
        }
        if (mCurAudioPlayer.isPlaying()) {
            mCurAudioPlayer.pause();
        }
        this.position = position;
        mPlayHolder = holder;
        Uri uri = Uri.parse("android.resource://" + AppUtils.getAppPackageName() + "/" + mBirdBeans.get(position).getRawId());
        mAudioPlayer.play(UriUtils.uri2File(uri).getPath());
        mPlayHolder.mIvPlayPause.setVisibility(View.VISIBLE);
        mPlayHolder.mRlImg.setVisibility(View.GONE);
        mPlayHolder.mIvPlayBg.setVisibility(View.VISIBLE);
    }

    private void onStartTimeChanged(ViewHolder holder, long startTime) {
        if (mPlayHolder == holder) {
            mAudioPlayer.seekTo((int) startTime);
            mAudioPlayer.resume();
        } else {
            if (mPlayHolder != null) {
                mPlayHolder.mIvPlayPause.setImageResource(R.drawable.icon_play2);
            }
            mPlayHolder = holder;
            Uri uri = Uri.parse("android.resource://" + AppUtils.getAppPackageName() + "/" + mBirdBeans.get(holder.mBindPos).getRawId());
            mAudioPlayer.play(UriUtils.uri2File(uri).getPath());
        }
    }

    private void onEndTimeChanged(ViewHolder holder, long endTime) {
        onStartTimeChanged(holder, endTime);
    }

    private void onStartEndTimeChanged(ViewHolder holder, long startTime, long endTime) {
        onStartTimeChanged(holder, startTime);
    }

    private IAudioPlayer.IListener mAudioPlayListener = new IAudioPlayer.IListener() {
        @Override
        public void onUpdatePlayTime(int curPlayTime, int duration) {

        }

        @Override
        public void onPlayChange(boolean isPlaying) {
            if (mPlayHolder != null) {
                mPlayHolder.mIvPlayPause.setImageResource(isPlaying ? R.drawable.icon_stop2 : R.drawable.icon_play2);
            }
        }
    };

    public class ViewHolder extends RecyclerView.ViewHolder {

        public MyLayout mRlItem;
        public TextView mTvName;
        public RelativeLayout mRlImg;
        public ImageView mIvImg;
        public ImageView mIvPlayPause;
        public ImageView mIvPlayBg;
        public int mBindPos;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
        }

        public void initView(View parent) {
            if (parent == null) return;
            mRlItem = parent.findViewById(R.id.rlSelBirdItem);
            mRlImg = parent.findViewById(R.id.rlSelBirdItemImg);
            mIvImg = parent.findViewById(R.id.ivSelBirdItemImg);
            mTvName = parent.findViewById(R.id.tvSelBirdItemName);
            mIvPlayPause = parent.findViewById(R.id.ivSelBirdItemPlay);
            mIvPlayBg = parent.findViewById(R.id.ivSelBirdItemPlayBg);
        }

        public void bindData(SelBirdBean bean, int position) {
            mBindPos = position;

            mIvImg.setImageResource(bean.getImg());
            mTvName.setText(bean.getName());

            mRlItem.setOnClickListener(v -> {
                handlePlayPause(this, position);
            });
        }
    }

    public List<ViewHolder> getHolderList() {
        // RecyclerView 中这个方法需要特殊处理，这里返回空列表
        // 实际使用时可以通过 RecyclerView 的 findViewHolderForAdapterPosition 获取
        return new ArrayList<>();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickDelete(int position);
    }

}
